package com.users.rn.rncommon.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

/**
 * @Description:
 * @Author: bluce
 * @Date: 2020/12/3 14:20
 */
public final class ZipFileUtil {
    private static final int BUFF_SIZE = 1024;

    private static ExecutorService mExecutor = Executors.newCachedThreadPool();

    public static void unzipFile(File file, String folderPath, UnZipProgress progress) {
        mExecutor.execute(new UnZipTask(file, folderPath, progress));
    }

    public interface UnZipProgress {
        int STATUS_COMPLETE = 0;
        int STATUS_FAILURE = 1;

        void progress(int status, int progress);
    }

    private static class UnZipTask implements Runnable {

        private File mZipFile;
        private String mFolderPath;
        private UnZipProgress mUnZipProgress;

        UnZipTask(File zipFile, String folderPath, UnZipProgress progress) {
            mZipFile = zipFile;
            mFolderPath = folderPath;
            mUnZipProgress = progress;
        }

        public void unzipFile(File zipFile, String folderPath) throws ZipException, IOException {
            File desDir = new File(folderPath);
            if (!desDir.exists()) {
                desDir.mkdirs();
            }
            ZipFile zf = new ZipFile(zipFile);
            for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
                ZipEntry entry = ((ZipEntry) entries.nextElement());
                if (entry.isDirectory()) {
                    continue;
                }
                InputStream in = zf.getInputStream(entry);
                String str = folderPath + File.separator + entry.getName();
                str = new String(str.getBytes(), "utf-8");
                File desFile = new File(str);
                if (!desFile.exists()) {
                    File fileParentDir = desFile.getParentFile();
                    if (!fileParentDir.exists()) {
                        fileParentDir.mkdirs();
                    }
                    desFile.createNewFile();
                }
                OutputStream out = new FileOutputStream(desFile);
                byte buffer[] = new byte[BUFF_SIZE];
                int realLength;
                while ((realLength = in.read(buffer)) > 0) {
                    out.write(buffer, 0, realLength);
                }
                in.close();
                out.close();
            }
            zf.close();
            if (mUnZipProgress != null) {
                mUnZipProgress.progress(UnZipProgress.STATUS_COMPLETE, 0);
            }
        }

        @Override
        public void run() {
            try {
                unzipFile(mZipFile, mFolderPath);
            } catch (Exception e) {
                if (mUnZipProgress != null) {
                    mUnZipProgress.progress(UnZipProgress.STATUS_FAILURE, 0);
                }
                e.printStackTrace();
            }
        }

    }
}
